package com.idnx.mynewsdemo.view;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.idnx.mynewsdemo.R;
import com.idnx.mynewsdemo.database.DataBaseBean;

public class RegisterActivity extends AppCompatActivity {
    private EditText userEdit,passEdit;
    private Button button1,button2;
    private DataBaseBean dataBaseBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        userEdit = findViewById(R.id.userEdit);
        passEdit = findViewById(R.id.passEdit);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        dataBaseBean = new DataBaseBean(this);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userEdit.getText().toString().isEmpty() || passEdit.getText().toString().isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请输入用户名或密码！", Toast.LENGTH_SHORT).show();
                } else {
                    boolean pd = dataBaseBean.registerUser(userEdit.getText().toString(), passEdit.getText().toString());
                    if (pd) {
                        Toast.makeText(RegisterActivity.this, "注册成功！请返回登录", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(RegisterActivity.this, "该用户名已被注册！", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}